<?php
class Category_Path
{
	protected $category;
	protected $ancestors;
	/**
	 * @param expected instance of Category type $category
	 */
	public function __construct($category){
		if(!$category instanceof Category){
			throw new Exception("An instance of Category type is expected.");
		}
		$this->category = $category;
		$this->ancestors = $category->getNode()->getAncestors();
		$this->parsePath();
	}
	
	public function getPathAsArray($self = false){
		if($self){
			array_merge($this->ancestors,$this->category);
		}
		return $this->ancestors;
	}
	
	/**
	 * return full path of category
	 * /tree/news/social 
	 * @param unknown_type $self
	 */
	public function getPath($self = false){
		$pahts = array_keys($this->getPathAsArray($self));
		return implode('/',$pahts);
	}
	
	private function parsePath(){
		
		if($this->category)
			$this->category = array($this->category->slug => $this->category->name);
			
		if($this->ancestors && count($this->ancestors) > 1){
			
			$this->ancestors = $this->ancestors->toArray();
			array_shift($this->ancestors); //remove root node
			
			$ancestors = array();
			
			foreach($this->ancestors as &$ancestor){
				$ancestors[$ancestor['slug']] = $ancestor['name'];
			}
			
			$this->ancestors = $ancestors;
		}else{
			
			//if only have root node ==> set ancestors to rull array
			$this->ancestors = array();
		}
	}
}